import { apiClient } from "./http-client"
import type { AxiosRequestConfig } from "axios"

type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"

const request = async <T>(
  method: HttpMethod,
  url: string,
  data?: Record<string, any>,
  config?: AxiosRequestConfig
): Promise<T> => {
  const response = await apiClient.request({
    method,
    url,
    data,
    ...config
  })

  return response.data as T
}

const get = <T>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<T> => {
  return request<T>("GET", url, undefined, { params, ...config })
}

const post = <T>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<T> => {
  return request<T>("POST", url, data, config)
}

const put = <T>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<T> => {
  return request<T>("PUT", url, data, config)
}

const patch = <T>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<T> => {
  return request<T>("PATCH", url, data, config)
}

export { get, post, put, patch }
